package com.foreverwin.jzymes.report.clamp.service.impl;

import com.foreverwin.jzymes.common.constant.Constants;
import com.foreverwin.jzymes.common.domain.CarrierScanLog;
import com.foreverwin.jzymes.common.service.ISiteAdviceService;
import com.foreverwin.jzymes.common.service.ISystemRuleAdviceService;
import com.foreverwin.jzymes.common.util.CarrierUtil;
import com.foreverwin.jzymes.report.clamp.mapper.ClampReportMapper;
import com.foreverwin.jzymes.report.clamp.service.IClampReportService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

@Service
public class ClampReportServiceImpl implements IClampReportService {
    @Autowired
    ClampReportMapper clampReportMapper;
    @Autowired
    ISiteAdviceService siteAdviceService;
    @Autowired
    ISystemRuleAdviceService systemRuleAdviceService;

    public String OPERATION_CLAMP;

    /**
     * 查找装夹信息
     *
     * @param jsonMap
     * @return
     */
    public List<CarrierScanLog> findClampReport(Map<String,Object> jsonMap ){
        String carrier = (String)jsonMap.get("carrier");
        String site = (String)jsonMap.get("site");
        String startDate = (String) jsonMap.get("startDate");
        String endDate = (String) jsonMap.get("endDate");

        /**加载系统规则*/
        preload( site );
        /**检查站点*/
        siteAdviceService.existSite( site );

        Date start = null;
        Date end = null;
        List<CarrierScanLog> carrierScanLogs = new ArrayList<>();
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        if ( !StringUtils.isBlank(carrier) ){
           carrier =  CarrierUtil.buildCarrier( site , carrier );

           carrierScanLogs = clampReportMapper.findClampReport( site , carrier ,start , end ,OPERATION_CLAMP );
        }else {
            if ( !StringUtils.isBlank( startDate ) ){
                try {
                    start = sdf.parse( startDate );
                } catch (ParseException e) {
                    e.printStackTrace();
                }
            }else {
                /**前一天对应的当前时间*/
                Date date=new Date();
                Calendar calendar = Calendar.getInstance();
                calendar.setTime(date);
                calendar.add(Calendar.DAY_OF_MONTH, -1);
                date = calendar.getTime();
                start = date;
            }

            if ( !StringUtils.isBlank( endDate ) ){
                try {
                    end = sdf.parse( endDate );
                } catch (ParseException e) {
                    e.printStackTrace();
                }
            }

            carrierScanLogs = clampReportMapper.findClampReport( site , carrier ,start , end ,OPERATION_CLAMP );
        }

        return  carrierScanLogs;
    }

    /**
     * 加载系统规则
     *
     * @param site
     */
    public void preload( String site ){

        /**获取系统规则装夹工序*/
        if(org.springframework.util.StringUtils.isEmpty( OPERATION_CLAMP )){
            OPERATION_CLAMP = systemRuleAdviceService.existSystemRule( site, Constants.SYSTEM_RULE_OPERATION_CLAMP );
        }

    }

}
